package com.atguigu.tingshu.order.strategy.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.order.strategy.TradeStrategy;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Package: com.atguigu.tingshu.order.strategy.impl
 *
 * @author xiongkunYang
 * @description: 策略接口具体实现类。订单项目类型为声音
 * @since 2025/6/26 11:12
 */
@Component(SystemConstant.ORDER_ITEM_TYPE_TRACK)
@Slf4j
public class TrackTradeStrategy implements TradeStrategy {

    private final AlbumFeignClient albumFeignClient;

    public TrackTradeStrategy(AlbumFeignClient albumFeignClient) {
        this.albumFeignClient = albumFeignClient;
    }

    @Override
    public OrderInfoVo processTrade(Long userId, TradeVo tradeVo) {
        log.info("订单项目类型为声音，处理声音订单...");
        OrderInfoVo orderInfoVo = new OrderInfoVo();
        // 远程调用获取待购买的声音列表
        Long trackId = tradeVo.getItemId();
        Integer trackCount = tradeVo.getTrackCount();
        Result<List<TrackInfo>> paidTrackInfoListResult = this.albumFeignClient.findPaidTrackInfoList(trackId, trackCount);
        if (ResultCodeEnum.FAIL.getCode().equals(paidTrackInfoListResult.getCode())) {
            throw new GuiguException(ResultCodeEnum.REMOTE_ERROR);
        }
        List<TrackInfo> paidTrackInfoList = paidTrackInfoListResult.getData();
        Assert.notEmpty(paidTrackInfoList, "用户：{} 在该专辑下没有符合购买要求声音", userId);

        // 远程调用获取专辑信息，获取声音单集价格
        Result<AlbumInfo> albumInfoResult = this.albumFeignClient.getAlbumInfoById(paidTrackInfoList.get(0).getAlbumId());
        if (ResultCodeEnum.FAIL.getCode().equals(albumInfoResult.getCode())) {
            throw new GuiguException(ResultCodeEnum.REMOTE_ERROR);
        }
        BigDecimal price = albumInfoResult.getData().getPrice();

        // 处理订单相关价格。购买声音没有优惠，故不处理 derateAmount
        BigDecimal originalAmount = price.multiply(BigDecimal.valueOf(trackCount));
        BigDecimal orderAmount = originalAmount;
        BigDecimal derateAmount = new BigDecimal("0.00");

        // 封装订单中商品明细列表
        List<OrderDetailVo> orderDetailVoList = paidTrackInfoList.stream()
                .map(trackInfo -> {
                    OrderDetailVo orderDetailVo = new OrderDetailVo();
                    orderDetailVo.setItemId(trackInfo.getId());
                    orderDetailVo.setItemName(trackInfo.getTrackTitle());
                    orderDetailVo.setItemUrl(trackInfo.getCoverUrl());
                    orderDetailVo.setItemPrice(price);
                    return orderDetailVo;
                })
                .toList();

        this.setParam(orderInfoVo, originalAmount, orderAmount, derateAmount, orderDetailVoList, new ArrayList<>());

        return orderInfoVo;
    }
}
